package com.entrogy.service.impl;

import com.entrogy.bean.GroupEntity;
import com.entrogy.bean.GroupSimpleEntity;
import com.entrogy.bean.MessageEntity;
import com.entrogy.bean.UserEntity;
import com.entrogy.dao.DepartmentMapper;
import com.entrogy.dao.GroupMapper;
import com.entrogy.dao.ParentMapper;
import com.entrogy.dao.UserMapper;
import com.entrogy.service.GroupService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author 许红松
 * @date 2019/5/30 0030 上午 10:39
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;// 群组dao接口
    @Autowired
    private DepartmentMapper departmentMapper;//部门管理dao接口

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ParentMapper parentMapper;
    /**
     *  新增群组
     * @param groupEntity
     * @return
     */
    @Override
    public   int  insertGroup(GroupEntity groupEntity){
        return groupMapper.insertGroup(groupEntity);
    };

    /**
     * 获取群组list
     * @return
     */
    @Override
    public List<GroupEntity> getGroupList(Map<String,Object> map){
        return groupMapper.getGroupList(map);
    };

    /**
     * 获取群组list 总数
     * @return
     */
    public int  getGroupListTotal(Map<String,Object> map){
        return groupMapper.getGroupListTotal(map);
    };


    /**
     *  删除群组根据id
     * @return
     */
    @Override
    public  int  deleteGroupById(int  id){
        return groupMapper.deleteGroupById(id);
    }


    /**
     *  查询家长的集合
     * @return
     */
    @Override
    public List<Map<String,Object>> getParentList(Map<String,Object> map){
        // 如果是年级性的家长列表
        int isGrade = 0;
        // 如果是班级性的家长列表
        int isClass = 0;
        // 如果是全校性的家长列表
        int isSchool = 0;
        // 点对点classId为404，不分班级性、年级性、全校性
        if (!map.get("classId").equals("404")){
            if (map.get("classId").equals("4")){
                isSchool = 1;
            }else if (((String)map.get("classId")).substring(4,5).equals("0")){
                isGrade = 1;
            }else {
                isClass = 1;
            }
            map.put("isGrade", isGrade);
            map.put("isClass", isClass);
            map.put("isSchool", isSchool);
        }
        return groupMapper.getParentList(map);
    }

    /**
     *  查询家长的集合总数
     * @return
     */
    @Override
    public int getParentListTotal(Map<String,Object> map){
        UserEntity userEntity=(UserEntity)(SecurityUtils.getSubject().getSession().getAttribute("currentUser"));
        if (userEntity!=null){
            int school_code=userEntity.getSchoolCode();
            int sid=departmentMapper.getPositionById(userEntity.getpId()).getSid();
            if (school_code!=1){
                map.put("school_code",school_code);
                if (sid==5){//代表班主任  查询本班的所有的学生父母
                    map.put("course_id",userEntity.getClassId());
                }
                else  if(sid==4){//年级组长
                    map.put("grade_id",userEntity.getClassId());
                }
                else  if(sid==0 || sid==1){
                    return 0;
                }
            }else{
                return groupMapper.getParentListTotal(map);
            }
        }
        return groupMapper.getParentListTotal(map);
    };


    /**
     *   获取教师集合
     * @param map
     * @return
     */
    @Override
    public List<UserEntity> getUserList(Map<String,Object> map){
        UserEntity userEntity=(UserEntity)(SecurityUtils.getSubject().getSession().getAttribute("currentUser"));
        if (userEntity!=null){
            int school_code=userEntity.getSchoolCode();
            int sid=departmentMapper.getPositionById(userEntity.getpId()).getSid();
            if (school_code!=1){
                map.put("school_code",school_code);
                if (sid == 15){ // 年级组长能够看到本年级的所有教师的联系方式
                    // 首先查询班主任列表
                    List<UserEntity> classTeacherList = groupMapper.listClassLeader(userEntity.getGradeId(), school_code);
                    // 然后查询这个年级的任课老师
                    List<UserEntity> teacherList = groupMapper.listTeacher(userEntity.getGradeId(), school_code);
                    // 去重
                    Iterator<UserEntity> iterator = classTeacherList.iterator();
                    while (iterator.hasNext()){
                        UserEntity user = iterator.next();
                        for (UserEntity tempUser : teacherList) {
                            if (user.getId().equals(tempUser.getId())){
                                iterator.remove();
                            }
                        }
                    }
                    classTeacherList.addAll(teacherList);
                    return classTeacherList;
                }
                else  if(sid == 16){ // 班主任能够查看到的本班科任老师的联系方式
                    List<UserEntity> classTeacherList = groupMapper.listClassTeacher(userEntity.getClassId(), school_code);
                    return classTeacherList;
                }
                else if(sid == 1 || sid == 2 || sid == 3 || sid == 5 || sid == 6 || sid == 9 || sid == 10 || sid == 12 || sid == 13 || sid == 18 || sid == 25){ // 职位id为主任啊、副主任这些能够顾查看所有教师的联系方式
                    return groupMapper.getUserList(map);
                }
            }else {
                return groupMapper.getUserList(map);
            }
        }
        return groupMapper.getUserList(map);
    }
    /**
     *   获取教师集合总数
     * @param map
     * @return
     */
    @Override
    public int  getUserListTotal(Map<String,Object> map){
        UserEntity userEntity=(UserEntity)(SecurityUtils.getSubject().getSession().getAttribute("currentUser"));
        if (userEntity!=null){
            int school_code=userEntity.getSchoolCode();
            int sid=departmentMapper.getPositionById(userEntity.getpId()).getSid();
            if (school_code!=1){
                map.put("school_code",school_code);
                if (sid==5){//代表班主任  查询本班的所有的学生父母
                    map.put("class_id",userEntity.getClassId());
                }
                else  if(sid==4){//年级组长
                    map.put("class_id",userEntity.getClassId());
                }
                else  if(sid==0 || sid==1){
                    return 0;
                }
            }else {
                return groupMapper.getUserListTotal(map);
            }
        }
        return groupMapper.getUserListTotal(map);
    };

    /**
     *  新增一个信息
     * @param messageEntity
     * @return
     */
    @Override
    public int insertMessage(MessageEntity messageEntity){
        return groupMapper.insertMessage(messageEntity);
    };


    /**
     * 根据id 查询群组消息
     * @param id
     * @return
     */
    @Override
    public  GroupEntity  getGroupById(int  id){
        return groupMapper.getGroupById(id);
    }

    @Override
    public boolean updateGroup(GroupSimpleEntity groupSimpleEntity) {
        List<String> userIds = groupSimpleEntity.getUserIds();
        StringBuilder groupUserName = new StringBuilder();
        StringBuilder staffList = new StringBuilder();
        GroupEntity oldGroup = groupMapper.getGroupById(groupSimpleEntity.getId());
        for (String userId : userIds) {
            if (oldGroup.getIstoTeacher() == 2){
                groupUserName.append(",").append(userMapper.getUserNameById(userId));
            }else {
                groupUserName.append(",").append(parentMapper.getParentName(userId));
            }
            staffList.append(",").append(userId);
        }
        groupSimpleEntity.setGroupSize(userIds.size())
                .setGroupUserName(groupUserName.toString().substring(1))
                .setStaffList(staffList.toString().substring(1));
        int update = groupMapper.updateGroup(groupSimpleEntity);
        return update == 1;
    }

    @Override
    public List<String> listGroupUser(Integer groupId) {
        GroupEntity group = groupMapper.getGroupById(groupId);
        List<String> groupUserList = new ArrayList<>();
        if (StringUtils.isNoneBlank(group.getStaffList())){
            String[] userIdList = group.getStaffList().split(",");
            groupUserList = Arrays.asList(userIdList);
        }
        return groupUserList;
    }
}
